package com.pitaya.avatar.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.pitaya.avatar.dao.ISchoolDAO;
import com.pitaya.avatar.domain.School;
import com.pitaya.avatar.util.SchoolMatcher;

public class SchoolDAO extends HibernateTemplate implements ISchoolDAO{

	public boolean delete(School school) {
		Session session = getSession();
		session.delete(school);
		
		return true;
	}

	public boolean insert(School school) {
		Session session = getSession();
		
		session.save(school);
		return true;
	}

	public List<School> query(School school) {
		List<School> list = new ArrayList<School>();
		Session session = getSession();
		list = session.createCriteria(School.class).add(Restrictions.like("name", school.getName())).list();
		return list;
	}

	public boolean update(School school) {
		Session session = getSession();
		School schoolOld = (School)session.get(School.class, school.getSid());
		if(SchoolMatcher.update(schoolOld, school)) {
			session.update(schoolOld);
			return true;
		}
		return false;
	}

}
